﻿using System;
using System.Linq;
using PokemonBattle.PokemonData.SQLite.DAL;
using PokemonBattle.PokemonData.Util;

namespace PokemonBattle.PokemonData.Rnd
{
    public static class RandomData
    {

        #region PokemonFilter
        public delegate int[] PokemonFilter(PokemonData[] pokemons);

        public static PokemonFilter filter = (data) =>
            {
                return (from pm in data
                        where pm.Bv.Sum() >= 500
                        select pm.Identity).ToArray();
            };
        #endregion

        public static TeamData GetTeam(Random random)
        {
            return GetTeam(filter, random);
        }

        public static TeamData GetSmogonTeam(Random random)
        {
            var team = new TeamData();
            team.CustomInfo.DataName = BattleData.CustomData.DataName;
            team.CustomInfo.DataHash = BattleData.CustomData.DataHash;

            var ids = DALRandomTeam.GetRndPmId().GetRandom(random, 6).ToArray();
            
            for (int i = 0; i < ids.Length; i++)
            {
                var pid = DALRandomTeam.GetPids(ids[i]).GetRandom(random);
                var pm = DALRandomTeam.GetSmogonPm(pid, random);
                team.Pokemons[i] = pm;
            }
            return team;
        }

        #region old

        public static TeamData GetTeam(PokemonFilter filter, Random random)
        {
            var team = new TeamData();
            team.CustomInfo.DataName = BattleData.CustomData.DataName;
            team.CustomInfo.DataHash = BattleData.CustomData.DataHash;
            var pokemons = BattleData.GetAllPokemons();
            //筛选Identity
            var arr = pokemons.GetRandom(random, 6).ToArray();

            for (int i = 0; i < arr.Length; i++)
            {
                var data = arr[i];
                team.Pokemons[i] = GetPokemon(data, random);
            }
            return team;
        }

        internal static PokemonCustomInfo GetPokemon(PokemonCustomInfo pm, Random random)
        {
            var data = BattleData.GetAllPokemons().First(p => p.Identity == pm.Identity);
            //pm.Nickname = data.NameBase;
            pm.LV = 100;
            pm.Iv.SetAll(31);
            pm.Gender = data.GetGender(random);
            pm.SelectedTrait = data.GetTrait(random);
            return pm;
        }

        internal static PokemonCustomInfo GetPokemon(PokemonData data, Random random)
        {
            PokemonCustomInfo pm = new PokemonCustomInfo();

            pm.Identity = data.Identity;
            pm.Nickname = data.NameBase;
            pm.LV = 100;

            pm.Iv.SetAll(31);
            pm.Ev.SetAll(85);

            pm.Gender = data.GetGender(random);
            pm.Character = (PokemonCharacter)random.Next(25);

            pm.Item = (Item)133;
            pm.SelectedTrait = data.GetTrait(random);

            if (data.Moves.Count <= 4)
            {
                for (int i = 0; i < data.Moves.Count; i++)
                {
                    pm.SelectedMoves[i] = data.Moves[i].MoveName;
                }
            }
            else
            {
                var list = (from move in data.Moves.GetRandom(random, 4) select move.MoveName).ToArray();

                //选出power最高技能
                var maxMove = (from MoveData mdata in BattleData.GetAllMoves()
                               where mdata.Type == data.Type1 || mdata.Type == data.Type2
                               join MoveLearnData ldata in data.Moves
                               on mdata.Name equals ldata.MoveName
                               into moves
                               orderby mdata.Power descending
                               from move in moves
                               select move).FirstOrDefault();

                if (maxMove != null && !list.Contains(maxMove.MoveName))
                {
                    list[0] = maxMove.MoveName;
                }

                for (int i = 0; i < 4; i++)
                {
                    pm.SelectedMoves[i] = list[i];
                }
            }
            return pm;
        }

        #endregion

    }
}